///////////////////////////////////////////////////////////
//  DataManager.cpp
//  Implementation of the Class DataManager
//  Created on:      07-Sep-2013 9:06:27 PM
//  Original author: Jakkaphan Aiemcharoenjit
///////////////////////////////////////////////////////////

#include "DataManager.h"
#include "DataXMLReader.h"
#include "DataSchemaXML.h"
#include "s3e.h"
#include "IOFile.h"
//#include "utf8_codecvt_facet.hpp"

using namespace Data;

DataManager* Data::DataManager::g_instace = NULL;

class xml_buffered_writer;

DataManager::DataManager()
{
  this->m_checkListPages = NULL;
  this->m_templatePages = NULL;
}


DataManager::~DataManager()
{
  if(this->m_checkListPages)
    delete this->m_checkListPages;

  if(this->m_templatePages)
    delete this->m_templatePages;

  this->m_checkListPages = NULL;
  this->m_templatePages = NULL;
}


IDataManager* DataManager::GetInstance()
{
  if(!g_instace)
    g_instace = new DataManager();
	return g_instace;
}


void DataManager::Release()
{
  if(g_instace)
    delete g_instace;
  g_instace = NULL;
}


IPages* DataManager::GetCheckListPages()
{
	return  this->m_checkListPages;
}


IPages* DataManager::GetTemplatePages()
{
	return  this->m_templatePages;
}


void DataManager::Open(const std::string& path)
{
  pugi::xml_document* xmlDoc = new pugi::xml_document();
  pugi::xml_parse_result xmlParse = xmlDoc->load_file(path.c_str()/*"data1.xml"*//*, pugi::parse_default, pugi::encoding_utf8*/);
  pugi::xml_node appNode = xmlDoc->first_child();
  pugi::xml_node_iterator itPagesNode = appNode.begin();
  while(itPagesNode != appNode.end())
  {
    pugi::xml_attribute nameAttr = itPagesNode->attribute(DataSchemaXML::NAME_ATTR.c_str());
    if(!_tcscmp(nameAttr.value(), DataSchemaXML::TEMPLATEPAGES_NAME.c_str()))
      this->m_templatePages = DataXMLReader::CreatePages(*itPagesNode);
    else
      this->m_checkListPages = DataXMLReader::CreatePages(*itPagesNode);
    itPagesNode++;
  }
  delete xmlDoc;

  if(this->m_templatePages == NULL)
    this->m_templatePages = DataFactory::CreatePages();
  if(this->m_checkListPages == NULL)
    this->m_checkListPages = DataFactory::CreatePages();
}


void DataManager::Save(const std::string& path)
{
  String_t xml = _T("<") + DataSchemaXML::APPNAME_ELEMENT + _T(">");
  if(this->m_templatePages)
    xml += this->m_templatePages->ToXML();
  if(this->m_checkListPages)
    xml += this->m_checkListPages->ToXML();
  xml += _T("</") + DataSchemaXML::APPNAME_ELEMENT + _T(">");

  // Save file with xml content

  /*pugi::xml_document* xmlDoc = new pugi::xml_document();
  bool err = xmlDoc->save_file("sample.xml", xml.c_str(), pugi::parse_default, pugi::encoding_utf8);
  delete xmlDoc;

  int capacity = 2048;
  */

  IOFile ioFile;
  ioFile.WriteFile(path.c_str()/*_T("data1.xml")*/, xml.c_str());

//  char* wideCharContent = const_cast<char*>(xml.c_str());
////  ucs2char* wideString = reinterpret_cast<unsigned short*>(wideCharContent);
//  unsigned int utf8BufferSize = strlen(wideCharContent);
//  char* utf8Buffer = (char*)s3eMalloc(utf8BufferSize);
//  //void* buffer;
//  //int iResult = IwUTF8ToWideChar(wideString, wcslen(wideCharContent), utf8Buffer, utf8BufferSize);
//  //if(iResult != -1)
//  {
//    memcpy(utf8Buffer, xml.c_str(), utf8BufferSize /** sizeof(char)*/);
//    s3eFile* theFile = s3eFileOpen("sample.xml", "wb");
//    int ieror = s3eFileWrite(/*wideCharContent*/utf8Buffer, utf8BufferSize/* * sizeof(char)*/, 1, theFile);
////    s3eFileError error = (s3eFileError)ieror;
//    //free(buffer);
//    s3eFree(utf8Buffer);
//    s3eFileClose(theFile);
//  }
}